(function($) {
/******************************
*
* Product Notification JS
*
*******************************/
    var $document = $(document);

	//Product Notification - login
	$document.delegate('.product-notification-login','click',function(ev) {
		ev.preventDefault();
        var target = $(ev.target).closest('form');
        if(target.length){
			ev.preventDefault();
			//var url = "http://localhost:81/is-bin/INTERSHOP.enfinity/WFS/TBLEU-TBLUK-Site/en_GB/-/GBP/RenderProductNotification-ProcessConsumerLogin";
			var url = target.attr('action');
			
			$.ajax({
				url: url,
				type: 'POST',
				async: false,
				data: target.serialize(),
				success: function(data){
					var jsonData = data.replace(/<!--[\s\S]*?-->/g, "");
					var obj = jQuery.parseJSON(jsonData);
					if(obj.action == "SIGNIN" && obj.success == 1) {
						$(target).closest('.kor-overlay').trigger('dialog:hide');	//Close the login modal
						var oldHref = $('a[name=notificationSignOn]').attr('href');	//Grab the old url
						$('.kor-content-contents').load(oldHref);	//Refresh the notification modal
					} else if(obj.action == "SIGNIN" && obj.success === 0) {
						var oldHref = $('a.product-notification-signin-link').attr('href');	//Grab the old url
						$('.product-notification-login').closest('.kor-content-contents').load(oldHref);	//Refresh the notification modal
					}
				}
            });
        }
        return false;
	});
	
	//Product Notification - signout
	$document.delegate('.product-notification-signout','click',function(ev) {
		ev.preventDefault();
		var url = $(this).attr('href');
		$.ajax({
			url: url,
			success: function(data){
				var jsonData = data.replace(/<!--[\s\S]*?-->/g, "");
				var obj = jQuery.parseJSON(jsonData);
				if(obj.action == "SIGNOUT" && obj.success == 1) {
					var oldHref = $('a[name=notificationSignOn]').attr('href');	//Grab the old url
					$('.kor-content-contents').load(oldHref);	//Refresh the notification modal
				}
			}
		});
        return false;
	});
	
	
})(jQuery, document);
